package org.jin.jinaicode.controller;

import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

import org.jin.jinaicode.annotation.AuthCheck;
import org.jin.jinaicode.common.BaseResponse;
import org.jin.jinaicode.common.DeleteRequest;
import org.jin.jinaicode.common.ResultUtils;
import org.jin.jinaicode.constant.UserConstant;
import org.jin.jinaicode.exception.BusinessException;
import org.jin.jinaicode.exception.ErrorCode;
import org.jin.jinaicode.model.dto.admin.UserAddDTO;
import org.jin.jinaicode.model.dto.admin.UserQueryDTO;
import org.jin.jinaicode.model.dto.admin.UserUpdateAdminDTO;
import org.jin.jinaicode.model.dto.admin.AppQueryAdminDTO;
import org.jin.jinaicode.model.dto.admin.AppUpdateAdminDTO;
import org.jin.jinaicode.model.dto.admin.AppFeatureDTO;
import org.jin.jinaicode.model.dto.user.AppQueryDTO;
import org.jin.jinaicode.model.vo.UserAdminVO;
import org.jin.jinaicode.model.vo.AppVO;
import org.jin.jinaicode.service.ManagerService;
import org.jin.jinaicode.service.AppService;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员接口
 */
@RestController
@RequestMapping("/manager")
@Slf4j
@Tag(name = "管理员接口")
public class ManagerController {

  @Resource
  private ManagerService managerService;

  @Resource
  private AppService appService;

  /**
   * 管理员添加用户
   */
  @PostMapping("/add")
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  @Operation(summary = "添加用户", description = "管理员添加用户")
  public BaseResponse<Long> addUser(@RequestBody UserAddDTO userAddDTO, HttpServletRequest request) {
    if (userAddDTO == null) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
    }
    return managerService.addUser(userAddDTO, request);
  }

  /**
   * 管理员删除用户
   */
  @PostMapping("/delete")
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  @Operation(summary = "删除用户", description = "管理员删除用户")
  public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
    if (deleteRequest == null || deleteRequest.getId() == null) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
    }
    return managerService.deleteUser(deleteRequest, request);
  }

  /**
   * 管理员更新用户
   */
  @PostMapping("/update")
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  @Operation(summary = "更新用户", description = "管理员更新用户信息")
  public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateAdminDTO userUpdateAdminDTO,
      HttpServletRequest request) {
    if (userUpdateAdminDTO == null || userUpdateAdminDTO.getId() == null) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
    }
    return managerService.updateUser(userUpdateAdminDTO, request);
  }

  /**
   * 管理员根据ID获取用户
   */
  @GetMapping("/getById")
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  @Operation(summary = "获取用户", description = "管理员根据ID获取用户详情")
  public BaseResponse<UserAdminVO> getUserById(@RequestParam("id") long id, HttpServletRequest request) {
    if (id <= 0) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "ID不合法");
    }
    return managerService.getUserById(id, request);
  }

  /**
   * 管理员分页获取用户列表
   */
  @PostMapping("/list")
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  @Operation(summary = "分页获取用户", description = "管理员分页获取用户列表")
  public BaseResponse<Page<UserAdminVO>> listUserByPage(@RequestBody UserQueryDTO userQueryDTO,
      HttpServletRequest request) {
    if (userQueryDTO == null) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
    }
    return managerService.listUserByPage(userQueryDTO, request);
  }

  /**
   * 检查当前用户是否为管理员
   */
  @GetMapping("/is-admin")
  @Operation(summary = "检查管理员", description = "检查当前用户是否为管理员")
  public BaseResponse<Boolean> isAdmin(HttpServletRequest request) {
    return ResultUtils.success(managerService.isAdmin(request));
  }

  // ==================== 应用管理接口 ====================

  /**
   * 管理员根据 id 删除任意应用
   */
  @PostMapping("/app/delete")
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  @Operation(summary = "删除应用", description = "管理员根据 id 删除任意应用")
  public BaseResponse<Boolean> deleteApp(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
    if (deleteRequest == null || deleteRequest.getId() == null) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
    }
    return appService.deleteAppByAdmin(deleteRequest, request);
  }

  /**
   * 管理员根据 id 更新任意应用
   */
  @PostMapping("/app/update")
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  @Operation(summary = "更新应用", description = "管理员根据 id 更新任意应用")
  public BaseResponse<Boolean> updateApp(@RequestBody AppUpdateAdminDTO appUpdateAdminDTO, HttpServletRequest request) {
    if (appUpdateAdminDTO == null || appUpdateAdminDTO.getId() == null) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
    }
    return appService.updateAppByAdmin(appUpdateAdminDTO, request);
  }

  /**
   * 管理员分页查询应用列表
   */
  @PostMapping("/app/list")
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  @Operation(summary = "分页获取应用", description = "管理员分页查询应用列表")
  public BaseResponse<Page<AppVO>> listAppByPage(@RequestBody AppQueryAdminDTO appQueryAdminDTO,
      HttpServletRequest request) {
    if (appQueryAdminDTO == null) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
    }
    return appService.listAppByPageByAdmin(appQueryAdminDTO, request);
  }

  /**
   * 管理员根据 id 查看应用详情
   */
  @GetMapping("/app/getById")
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  @Operation(summary = "获取应用", description = "管理员根据 id 查看应用详情")
  public BaseResponse<AppVO> getAppById(@RequestParam("id") long id, HttpServletRequest request) {
    if (id <= 0) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "ID不合法");
    }
    return appService.getAppByIdByAdmin(id, request);
  }

  /**
   * 管理员设置/取消精选应用
   */
  @PostMapping("/app/feature")
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  @Operation(summary = "设置精选应用", description = "管理员设置或取消应用精选状态")
  public BaseResponse<Boolean> setAppFeature(@RequestBody AppFeatureDTO appFeatureDTO, HttpServletRequest request) {
    if (appFeatureDTO == null || appFeatureDTO.getId() == null) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
    }
    return appService.setAppFeature(appFeatureDTO, request);
  }

  /**
   * 管理员分页查询所有精选应用列表
   */
  @PostMapping("/app/list/featured")
  @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
  @Operation(summary = "分页获取精选应用", description = "管理员分页查询所有精选应用列表")
  public BaseResponse<Page<AppVO>> listFeaturedAppByPage(@RequestBody AppQueryDTO appQueryDTO,
      HttpServletRequest request) {
    if (appQueryDTO == null) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
    }
    return appService.listAllFeaturedAppByPage(appQueryDTO);
  }
}
